package interfazeak;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;


import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import laguntzaileak.Botikaaukeratua;

import db.BiltegiaKud;
import db.BotikaKud;
import db.OsatuKud;

import Taulak.MTBBotika;

public class Botikak extends JFrame {

	private static final Mahastiak_Lekuak Mahastiak_Lekuak = null;
	private JPanel contentPane;
	private JTable table;
	private MTBBotika botikataula;
	private JButton lekua;
	private JButton btnBotikak;
	private JButton btnHasieraOrria;
	private JButton gehitu;
	private JButton ezabatu;
	private JButton btnOharra;
	private JButton btnAlarmaSortu;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Botikak frame = new Botikak();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @param mahastiak_Lekuak
	 */
	public Botikak() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 500);
		setTitle("BOTIKAK");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		btnHasieraOrria = new JButton("HASIERA ORRIA");
		btnHasieraOrria.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				Botikak.this.buelta();

			}
		});
		btnHasieraOrria.setBounds(580, 64, 157, 25);
		contentPane.add(btnHasieraOrria);

		btnOharra = new JButton("OHARRAK");
		btnOharra.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				JDialog dialog = new Oharrak(Botikak.this, true);
				dialog.setVisible(true);
			}
		});
		btnOharra.setBounds(423, 64, 157, 25);
		contentPane.add(btnOharra);

		btnAlarmaSortu = new JButton("ALARMA SORTU");
		btnAlarmaSortu.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				Botikak.this.alarmaSortu();
			}
		});
		btnAlarmaSortu.setBounds(266, 64, 157, 25);
		contentPane.add(btnAlarmaSortu);

		btnBotikak = new JButton("BOTIKAK");
		btnBotikak.setForeground(UIManager.getColor("Button.select"));
		btnBotikak.setBounds(25, 64, 117, 25);
		contentPane.add(btnBotikak);

		lekua = new JButton("LEKUAK");
		lekua.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Botikak.this.lekurajoan();
			}
		});

		lekua.setBounds(142, 64, 117, 25);
		contentPane.add(lekua);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(96, 190, 350, 185);
		contentPane.add(scrollPane);

		table = new JTable();
		botikataula = new MTBBotika();
		table.setModel(botikataula);

		// mahastitaula.kargatu("Lezama","Etxea"); // proba bat da gehitzeko.
		// mahastitaula.kargatu("Derio", "Errekalde");// proba bat da gehitzeko.

		scrollPane.setViewportView(table);

		gehitu = new JButton("GEHITU");
		gehitu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				Botikak.this.gehituBotika();

			}

		});
		gehitu.setForeground(Color.BLACK);
		gehitu.setBounds(450, 220, 117, 25);
		contentPane.add(gehitu);

		ezabatu = new JButton("EZABATU");
		ezabatu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRow() == -1) {
					JOptionPane.showMessageDialog(Botikak.this,
							"Lerro bat aukeratu", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
				} else {
					int konfirmatu = JOptionPane.showConfirmDialog(
							Botikak.this, "Ezabatu nahi duzu?");

					if (JOptionPane.OK_OPTION == konfirmatu) {
						System.out.println("confirmado");
						Botikak.this.ezabatu();
					}
				}

			}

		});
		ezabatu.setBounds(450, 270, 117, 25);
		contentPane.add(ezabatu);
		bistaratu();
	}

	protected void alarmaSortu() {
		
		JDialog dialog = new Alarma(Botikak.this, true);
		dialog.setVisible(true);
	}

	protected void buelta() {
		
		Hasiera h = new Hasiera();
		h.setVisible(true);
		this.dispose();
	}

	protected void ezabatu() {
		
		int lerroa = table.getSelectedRow();
		String trat = (String) table.getValueAt(lerroa, 0);
		String izurri = (String) table.getValueAt(lerroa, 1);
		String data =   (String) table.getValueAt(lerroa, 2);
		int id = OsatuKud.lortuIdOsatu(trat,izurri,data);
		OsatuKud.ezabatu(id);
		botikataula.ezabatu(lerroa);
	}

	protected void bistaratu() {

		ResultSet rs = OsatuKud.kargatu();
		try {
			while (rs.next()) {
				String tratamendua = rs.getString("tratamendua");
				String izurria = rs.getString("izurria");
				String data = rs.getString("data");
				botikataula.kargatu(tratamendua, izurria, data);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
	}

	protected void gehituBotika() {
	
		JDialog dialog = new GehituBotikak(Botikak.this, true);
		dialog.setVisible(true);

	}

	protected void lekurajoan() {
		Mahastiak_Lekuak b = new Mahastiak_Lekuak();
		b.setVisible(true);
		this.dispose();

	}

	public void sartuBotika(Botikaaukeratua botikabektore) {
		
		OsatuKud.tratamenduasartu(botikabektore);
	}

	public void ezabatutaula() {
		
		int ler = table.getRowCount();
		botikataula.taulaHasieratu(ler);
	}

}
